﻿window['ajaxPopupIns'] = [];
function confirmSelItem(i,v,n){
	var s = window['ajaxPopupIns'][i];
	if(s){
		s.valueH.value = v;
		s.nameH.value = n;
		s.showTxtOnHTML();
		hideCurrentPopup();
	}
}

function initAJAXPopup(){
	var e=/<[^>]+\srdmodule=[\'\"]?([\w|]+)[^>]+/g;
	var g=/id=[\'\"]?([\w\-]+)/i;
	var i,j;
	window.document.body.innerHTML.replace(e,function(a,b){if(b=="ajaxPopup"&&(i=a.match(g))&&(j=document.getElementById(i[1]))){
																window['ajaxPopupIns'][i[1]] = new ajaxPopupFac(j);
																window['ajaxPopupIns'][i[1]].showTxtOnHTML();
															}
										});
}

function ajaxPopupFac(h){
	var s = this;
	s.html = h;
	s.id = s.html.id;
	s.html.onfocus = function(){this.blur();};
	s.vHiddenId = s.html.getAttribute('vHiddenId')||null;
	s.nHiddenId = s.html.getAttribute('nHiddenId')||null;
	s.valueH = s.vHiddenId&&document.getElementById(s.vHiddenId)?document.getElementById(s.vHiddenId):null;
	s.nameH = s.nHiddenId&&document.getElementById(s.nHiddenId)?document.getElementById(s.nHiddenId):null;
	s.width = s.html.getAttribute('nWidth')||100;
	s.maxW = s.html.getAttribute('maxWidth')||null;
	s.maxH = s.html.getAttribute('maxHeight')||100;
	s.clearFn = s.html.getAttribute('clearFn')||null;
	s.title = s.html.getAttribute('modTitle')||'';
	s.txtNull = s.html.getAttribute('modTxtNull')||'请选择';
	s.txtPre = s.html.getAttribute('modTxtPre')||'';
	s.showL = s.html.getAttribute('showLen')||10;
	s.ajaxASP = s.html.getAttribute('ajaxASP')||null;
	s.cache4get = s.html.getAttribute('cache4get')||'yes';
	s.ajaxData = s.html.getAttribute('ajaxData')||'';
	var myDiv = document.createElement('div');
	myDiv.id = s.html.id + '_div';
	myDiv.className = 'popupSearcher';
	myDiv.style.width = s.width + 'px';
	var subDiv = document.createElement('div');
	subDiv.className = 'title';
	subDiv.innerHTML = '<div style="float:left;">'+s.title+'：</div><div style="float:right;"><a href="javascript:confirmSelItem(\''+s.id+'\',\'\',\'\');'+(s.clearFn?s.clearFn:'')+'">[清空]</a> <a href="javascript:hideCurrentPopup();">[关闭]</a></div>';
	myDiv.appendChild(subDiv);
	subDiv = document.createElement('div');
	subDiv.className = 'content';
	myDiv.appendChild(subDiv);
	s.div = myDiv;
	s.div_c = subDiv;
	subDiv = document.createElement('div');
	s.div_c.appendChild(subDiv);
	s.div_t = subDiv;//fix IE7
	s.div.onclick = function(event){var e = event?event:window.event;e.preventDefault?e.stopPropagation():(e.cancelBubble=true);};
	if(s.html.offsetParent&&s.html.offsetParent.tagName.toLowerCase()!='html') s.html.offsetParent.appendChild(s.div);
	else document.body.appendChild(s.div);
	s.data = null;
	s.ajaxState = 'ok';
	myAttachEvent(s.html,'click',function(e){s.popupItems(e);});
}
ajaxPopupFac.prototype = {
	popupItems : function(e){
		var s = this;
		var coord = getXY(s.html);
		var x = coord.x;
		var y = coord.y + parseFloat(s.html.offsetHeight);
		var rW,rH;
		if(s.data==null){//goto ajax
			s.div_t.innerHTML = '<div style="padding:5px;">请稍候...</div>';
			switchVisibility(s.div.id,e,x,y);
			
			if(s.ajaxState=='ajax') return;
			var data = s.ajaxData;
			data += (data!=''&&s.cache4get=='no'?'&':'') + (s.cache4get=='no'?'f='+new Date().valueOf().toString():'');
			submitCallback(data,s.ajaxASP, handleGetSearcherResponse,'get',s);
		}
		else{
			switchVisibility(s.div.id,e,x,y);
		}
	},
	showTxtOnHTML : function(){
		var s = this;
		var txt = s.nameH.value==''?s.txtNull:s.txtPre+s.nameH.value;
		var fixedTxt = txt.fixByLen(s.showL);
		s.html.value = fixedTxt;
		if(txt!=fixedTxt) s.html.title = txt;
	}
}

function handleGetSearcherResponse(success,responseText,objPar){
	objPar.ajaxState = 'ok';
	if(!document.all) objPar.div.style.width = 'auto'; // fix FireFox
	if(success){
		objPar.data = responseText;
		if(responseText=='') objPar.div_t.innerHTML = '<div style="padding:5px;">没有' + objPar.title + '</div>';
		else objPar.div_t.innerHTML = responseText;
	}
	else{
		objPar.div_t.innerHTML = '<div style="padding:5px;">ajax 出错！请稍候再试！</div>';
	}
	var rW = parseFloat(objPar.div_t.offsetWidth);
	if(!document.all) objPar.div.style.width = objPar.width + 'px'; // fix FireFox
	var rH = parseFloat(objPar.div_t.offsetHeight);
	objPar.div_c.style.height = Math.min(rH,objPar.maxH)+'px';
	if(objPar.maxW!=null&&!isNaN(objPar.maxW)){
		var scrollHFlag = Math.max(rW,objPar.width)>objPar.maxW;
		var okW = Math.min((Math.max(rW,objPar.width)),objPar.maxW);
		objPar.div_c.style.width = okW+'px';
		objPar.div.style.width = okW + 'px';
		if(scrollHFlag) objPar.div_c.style.height = parseFloat(objPar.div_c.style.height)+20+'px';
	}
	if(objPar.div.shim&&objPar.div.style.visibility!='hidden') objPar.div.shim.setStyle('visibility','visible');
}

$regEventDomReady(initAJAXPopup);

function $regEventDomReady(fn){var userAgent=navigator.userAgent.toLowerCase();var domReadyUtil={};domReadyUtil.browser={version:(userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/)||[])[1],safari:/webkit/.test(userAgent),opera:/opera/.test(userAgent),msie:/msie/.test(userAgent)&&!/opera/.test(userAgent),mozilla:/mozilla/.test(userAgent)&&!/(compatible|webkit)/.test(userAgent)};domReadyUtil.readyList=[];domReadyUtil.each=function(object,callback,args){var name,i=0,length=object.length;if(args){if(length==undefined){for(name in object)if(callback.apply(object[name],args)===false)break}else for(;i<length;)if(callback.apply(object[i++],args)===false)break}else{if(length==undefined){for(name in object)if(callback.call(object[name],name,object[name])===false)break}else for(var value=object[0];i<length&&callback.call(value,i,value)!==false;value=object[++i]){}}return object};domReadyUtil.ready=function(){if(!domReadyUtil.isReady){domReadyUtil.isReady=true;if(domReadyUtil.readyList){domReadyUtil.each(domReadyUtil.readyList,function(){this.call(document)});domReadyUtil.readyList=null}}};var readyBound=false;function bindReady(){if(readyBound)return;readyBound=true;if(document.addEventListener&&!domReadyUtil.browser.opera)document.addEventListener("DOMContentLoaded",domReadyUtil.ready,false);if(domReadyUtil.browser.msie&&window==top)(function(){if(domReadyUtil.isReady)return;try{document.documentElement.doScroll("left")}catch(error){setTimeout(arguments.callee,0);return}domReadyUtil.ready()})();if(domReadyUtil.browser.opera)document.addEventListener("DOMContentLoaded",function(){if(domReadyUtil.isReady)return;for(var i=0;i<document.styleSheets.length;i++)if(document.styleSheets[i].disabled){setTimeout(arguments.callee,0);return}domReadyUtil.ready()},false);myAttachEvent(window,"load",domReadyUtil.ready)};bindReady();if(domReadyUtil.isReady)fn.call(document,domReadyUtil);else domReadyUtil.readyList.push(function(){return fn.call(this,domReadyUtil)});return this}

String.prototype.fixLength=function(){return this.replace(/[^\x00-\xff]/g,"aa").length;}
String.prototype.fixByLen=function(l){
	if(this.fixLength()<=l) return this;
	var fixedStr = '';
	var c,i,n=0;
	for(i=0;i<this.length;i++){
		if(n<l){
			c = this.charAt(i);
			if(/[^\x00-\xff]/.test(c)) n += 2;
			else n += 1;
			fixedStr += c;
		}
		else break;
	}
	return fixedStr+'...';
}